<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die( 'Restricted access' ); 

jimport( 'joomla.application.component.view');                      

class jZmluvyViewZmluvy extends JView 
{ 
    function display($tpl = null) 
    { 
    global $mainframe, $option;
    $db				=& JFactory::getDBO();
    $params	= JComponentHelper::getParams('com_jzmluvy');
    $nastavenie = &jzmluvyHelper::nastavenie();
    $document 	= & JFactory::getDocument();
    $document->addStyleSheet($this->baseurl.'/components/com_jzmluvy/assets/css/jzmluvy_modra.css');
    $item     = & $this->get( 'Data');
       
    // Get data from the model
    $items =& $this->get('Data');      
    $pagination =& $this->get('Pagination');
    $filter_rok		= $mainframe->getUserStateFromRequest( $context.'filter_rok',		'filter_rok', 	'1',			'string' );
    $filter_mesiac= $mainframe->getUserStateFromRequest( $context.'filter_mesiac',		'filter_mesiac', 	'1',			'string' );
    
    // vypis dodatkov k zmluve
    $query = 'SELECT *'
			. ' FROM #__jzmluvy'
      . ' WHERE catid = 2 AND typ = 1 AND sposob_uhrady = '.$item[0]->id
      . ' ORDER BY datZver ASC'
		;
 		$db->setQuery( $query );		
    $dodatok = $db->loadObjectList();
    
    // generovanie rolovatka (listu) pre filtrovanie podla roku
    $query = 'SELECT DISTINCT YEAR(datZver) as vyber_rok'
			. ' FROM #__jzmluvy'
      . ' WHERE catid = 2'
      . ' ORDER BY vyber_rok DESC'
		;
 		$db->setQuery( $query );		

    if ($filter_rok == '') :
	  $rok[] 		= JHTML::_('select.option',  '', JText::_('VSETKY_ROKY') );
    else :
    $rok[] 		= JHTML::_('select.option',  '', JText::_('VYBER_ROK') );
    endif;
    
    foreach( $db->loadObjectList() as $obj )
		{
      // select.option, vratena hodnota, zobrazena hodnota
      $rok[] = JHTML::_('select.option',  $obj->vyber_rok, $obj->vyber_rok);
		}
		$lists['rok'] 	= JHTML::_('select.genericlist',   $rok, 'filter_rok', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $filter_rok );

    // generovanie rolovatka (listu) pre filtrovanie podla mesiaca
    $query1 = 'SELECT DISTINCT MONTH(datZver) as vyber_mesiac'
			. ' FROM #__jzmluvy'
      . ' WHERE catid = 2 ORDER BY vyber_mesiac ASC'
    ;
 		$db->setQuery( $query1 );		
		
		if ($filter_mesiac == '') :
      $mesiac[] 		= JHTML::_('select.option',  '', JText::_('VSETKY_MESIACE') );
    else :
		  $mesiac[] 		= JHTML::_('select.option',  '', JText::_('VYBER_MESIAC') );
    endif;
    
    foreach( $db->loadObjectList() as $obj )
		{
      $nazov = array(1 =>'Január','Február','Marec','Apríl','Máj','Jún','Júl','August','September','Október','November','December' );
      $mesiac_nazov = $nazov[$obj->vyber_mesiac];
      // select.option, vratena hodnota, zobrazena hodnota
      $mesiac[] = JHTML::_('select.option', $obj->vyber_mesiac,  $mesiac_nazov);
      //echo utf8_encode(strftime('%B'));
		}
		$lists['mesiac'] 	= JHTML::_('select.genericlist',   $mesiac, 'filter_mesiac', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $filter_mesiac );
    
    // push data into the template
    $this->assignRef('items', $items);     
    $this->assignRef('pagination', $pagination);
    $this->assignRef('params',		$params);
    $this->assignRef('lists', $lists );
    $this->assignRef('nastavenie', $nastavenie );
    $this->assignRef('dodatok', $dodatok );    
    
    //*** pridavane pre triedenie
    /* Call the state object */
    $state =& $this->get( 'state' );
    
    /* Get the values from the state object that were inserted in the model's construct function */
    $lists['order_Dir'] = $state->get( 'filter_order_Dir' );
    $lists['order']     = $state->get( 'filter_order' );
    
 //*** zadefinovanie sposobu uhrady
	function typ_zml_dod ($typ) {
     if ( $typ == 0 ) { 
     $output = JText::_( 'ZM_EDIT_ZMLUVA' );
      }
      else if ( $typ == 1 ) { 
      $output = JText::_( 'ZM_EDIT_DODATOK' ); 
     }
      return $output;
      }

     parent::display($tpl);
    }
    
}
?>